using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class PixelPerfectCanvasScaler:MonoBehaviour {
	[SerializeField] Vector2Int referenceResoloution;
	UnityEngine.UI.CanvasScaler canvasScaler;

	private void Start() {
		canvasScaler=GetComponent<UnityEngine.UI.CanvasScaler>();
	}

	private void Update() {

		Camera currentCamera = Camera.main;
		if(currentCamera==null) currentCamera=FindObjectOfType<Camera>();

		Vector2Int currentResoloution = new Vector2Int(currentCamera.pixelWidth,currentCamera.pixelHeight);

		int multiplyX = currentResoloution.x/referenceResoloution.x;
		int multiplyY = currentResoloution.y/referenceResoloution.y;

		int scaleFactor = Mathf.Min(multiplyX,multiplyY);
		canvasScaler.scaleFactor=scaleFactor;

	}

}
